<template>
	<view class="center">
		<view class="center_top">
			<view class="mask"></view>
		</view>
		<view class="center_box_bg">
			<view class="profily">
				<view class="base" @click="handleUser">
					<view class="profily_header" :style="avatarBg">
						<!-- 头像是背景 -->
					</view>
					<text>{{userInfo?userInfo.username:'尚未登陆'}}</text>
					<image src="../../static/fumou-center-template/setting.png" mode=""></image>
				</view>
				<view class="order_status">
					<view class="status" v-for="item in status">
						<image class="icon" :src="item.url" mode="aspectFill"></image>
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
			<view class="baiban">

			</view>
			<view class="center_menu">
				<view class="menu_item" v-for="item in menus">
					<image :src="item.icon" mode="aspectFill"></image>
					<text>{{item.name}}</text>
				</view>
			</view>
		</view>
		<view class="padding">
			<button @click="handleLogout" class="cu-button bg-brown block">
				退出登录
			</button>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import {userUpdate} from '@/api/user.js'
	import {pathToBase64} from '@/js_sdk/mmmm-image-tools/index.js'
	// #ifdef H5
	import AV from 'leancloud-storage'
	// #endif
	// #ifdef MP-WEIXIN
	import AV from '../../js_sdk/leancloud-wx-sdk/av-core-min.js'
	// #endif
	export default {
		data() {
			return {
				status: [{
						key: 1,
						name: '待发货',
						url: '../../static/fumou-center-template/one.png'
					},
					{
						key: 2,
						name: '待收货',
						url: '../../static/fumou-center-template/2.png'
					},
					{
						key: 3,
						name: '待评价',
						url: '../../static/fumou-center-template/3.png'
					},
					{
						key: 4,
						name: '全部订单',
						url: '../../static/fumou-center-template/4.png'
					}
				],
				menus: [{
						name: '我的收藏',
						icon: '../../static/fumou-center-template/5.png',
						key: 1,
					},
					{
						name: '地址管理',
						icon: '../../static/fumou-center-template/6.png',
						key: 2,
					},
					{
						name: '尺码对照表',
						icon: '../../static/fumou-center-template/7.png',
						key: 3,
					},
					{
						name: '帮助中心',
						icon: '../../static/fumou-center-template/8.png',
						key: 4,
					},
					{
						name: '意见反馈',
						icon: '../../static/fumou-center-template/9.png',
						key: 5,
					},
					{
						name: '关于我们',
						icon: '../../static/fumou-center-template/10.png',
						key: 6,
					}

				]
			};
		},
		computed: {
			...mapState({
				userInfo:state=>state.user.userInfo
			}),
			avatarBg(){ //动态生成头像背景
				let url = '../../static/fumou-center-template/header.jpg'
				let {userInfo} = this
				if(userInfo&&userInfo.avatar){
					url = userInfo.avatar
				}
				return `background-image: url(${url})`
			}
		},
		methods: {
			handleLogout(){ //退出登陆
				uni.removeStorage({
					key:'userInfo'
				})
				this.$store.commit('user/initUserInfoMut',null)
			},
			handleUser(){
				if(this.userInfo){
					uni.chooseImage({
						count:1,
						success: (file) => {
							// console.log(file);
							let temp = file.tempFilePaths[0]
							// #ifdef H5
							this.webUpload(temp)
							// #endif
							// #ifdef MP-WEIXIN
							this.wxUpload(temp)
							// #endif
						}
					})
					return
				}
				uni.navigateTo({
					url:'/pages/login/login'
				})
			},
			webUpload(temp){ //web端图片上传
				pathToBase64(temp).then(base64=>{
					// console.log(base64);
					new AV.File('avatar.png',{base64}).save().then(async(res)=>{
						console.log(res);
						let {url} = res.attributes
						await userUpdate({avatar:url})
						this.$store.commit('user/initUserInfoMut',{
							...this.userInfo,
							avatar:url
						})
						uni.setStorage({
							key:'userInfo',
							data:{
								...this.userInfo,
								avatar:url
							}
						})
					})
				})
			},
			wxUpload(temp){ //微信端图片上传
				console.log(123);
				new AV.File('avatar.png', {
				  blob: {
					uri: temp,
				  },
				})
				.save()// 上传
				.then(res => {// 上传成功
				   console.log(res);
			    })
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.profily,
	.profily_header {
		border-radius: 8px;
	}

	.center {
		height: 100%;

		&_top {
			height: 18%;
			background: url('../../static/fumou-center-template/header.jpg') no-repeat 0% 50%;
			background-size: cover;

			// background: #E6E6E6;
			.mask {
				background: rgba(0, 0, 0, .4);
				height: 100%;
			}
		}

		&_box_bg {
			background: #F9F9F9;
			position: relative;

			.profily {
				position: absolute;
				width: 90%;
				// border:1px solid #F7F7F7;
				margin: 0 auto;
				top: -100upx;
				left: 5%;
				background: #FEFEFE;
				padding: 35upx;
				box-sizing: border-box;
				box-shadow: 0px 2px 5px #EDEDED;
			}
		}
	}

	.base {
		display: flex;
		align-items: center;
		border-bottom: 2px solid #F6F6F6;
		padding-bottom: 35upx;
		position: relative;
		.profily_header {
			height: 120upx;
			width: 120upx;
			background-image: url('../../static/fumou-center-template/header.jpg');
			background-size: 100%;
		}

		text {
			margin-left: 20px;
			font-size: 30upx;
		}
		
		image{
			position: absolute;
			height: 40upx;
			width: 40upx;
			right: 0px;
			top:0px;
		}
	}

	.order_status {
		display: flex;
		justify-content: space-between;
		margin-top: 35upx;

		.status {
			width: 140upx;
			font-size: 24upx;
			text-align: center;
			letter-spacing: .5px;
			display: flex;
			flex-direction: column;
			.icon {
				width: 50upx;
				height: 50upx;
				margin: 0 auto;
				margin-bottom: 5px;
				
			}
		}
	}

	.baiban {
		background: #FEFEFE;
		height: 300upx;
	}

	.center_menu {
		width: 100%;
		display: inline-block;

		.menu_item {
			font-size: 28upx;
			letter-spacing: 1px;
			padding: 14px 5%;
			background: #FEFEFE;
			overflow: hidden;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			position: relative;
			border-bottom: 1px solid #EFEFEF;

			&:hover {
				background: #F6F6F6 !important;
			}

			&::after {
				content: '';
				width: 30upx;
				height: 30upx;
				position: absolute;
				right: 5%;
				background: url('../../static/fumou-center-template/right.png') no-repeat;
				background-size: 100%;
			}

			text:nth-of-type(1) {
				margin-left: 10px;
			}

			image {
				width: 40upx;
				height: 40upx;
			}

			&:nth-of-type(4) {
				margin-top: 10px;
			}
		}
	}
</style>
